//
//  HHWebViewController.m
//  HHBroker
//
//  Created by He Wei on 9/17/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHWebViewController.h"

@interface HHWebViewController () <UIWebViewDelegate>

{
    UIWebView *_webView;
}
@end

@implementation HHWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.naviTitle) {
        self.title = self.naviTitle;
    }
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    
    if (self.urlString) {
        NSURL *URL = [NSURL URLWithString:self.urlString];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        [_webView loadRequest:request];
    }
    
    if (self.fileString) {
        
        NSData *data = [NSData dataWithContentsOfFile:self.fileString];
        NSString *pathType = [self.fileString pathExtension];
        NSString *MIMEType;
        if ([pathType isEqualToString:@"html"]) {
            MIMEType = @"text/html";
        }
        MIMEType = @"text/plain";
        
        [_webView loadData:data MIMEType:MIMEType textEncodingName:@"UTF-8" baseURL:nil];
    }
    
    _webView.delegate = self;
    [self.view addSubview:_webView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

#pragma mark - UIWebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    MBProgressHUD *hub = [MBProgressHUD HUDForView:self.view];
    hub.mode = MBProgressHUDModeText;
    hub.labelText = NSLocalizedString(@"网络好像有点问题", @"");
    [hub hide:YES afterDelay:1];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    MBProgressHUD *hub = [MBProgressHUD HUDForView:self.view];
    [hub hide:NO];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
